package cn.com.huzo.webapp.controller.oa;

import cn.com.huzo.model.entities.oa.MyCalendarEntity;
import cn.com.huzo.service.oa.MyCalendarManager;
import huzofw.common.web.controller.BaseFormController;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.collections4.MapUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * 个人日程表
 *
 * @author 吴旭
 */
@Controller
@RequestMapping(value = "/oa/mycalendar")
public class MyCalendarController extends BaseFormController {

    // <editor-fold defaultstate="collapsed" desc="属性">
    @Resource(name = "MyCalendarManager")
    private MyCalendarManager _manager;
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="画面处理部分">
    /**
     * 跳转到日程表一览画面
     * <p>
     * @param request
     * @return
     * @throws java.lang.Exception
     */
    @RequestMapping("/list.html")
    public ModelAndView redirectListWnd(HttpServletRequest request) throws Exception {
        ModelAndView view = new ModelAndView();
        view.setViewName("oa.mycalendar.list.def");
        return view;
    }

    /**
     * 跳转到日程表编辑画面
     *
     * @return
     */
    @RequestMapping("/edit.html")
    public ModelAndView redirectEditPopupWnd() {
        ModelAndView view = new ModelAndView();
        view.setViewName("popup.oa.mycalendar.edit.def");
        return view;
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="数据处理部分">
    /**
     * 取得该日程详细。
     *
     * @param criterion 用户标识、起止时间
     * @return 日程数据。
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "/fetchByCalendarId.html",
            method = RequestMethod.POST)
    @ResponseBody
    public MyCalendarEntity fetchByCalendarId(@RequestBody final Map<String, Object> criterion) throws Exception {
        return _manager.fetchByKey(MapUtils.getString(criterion, "CalendarId"));
    }

    /**
     * 取得该用户指定时间段的日程数据。
     *
     * @param criterion 用户标识、起止时间
     * @return 日程数据。
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "/fetchCalendar.html",
            method = RequestMethod.POST)
    @ResponseBody
    public List<MyCalendarEntity> fetchCalendar(@RequestBody final Map<String, Object> criterion) throws Exception {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        List<MyCalendarEntity> _result = _manager.fetchCalendar(MapUtils.getLong(criterion, "UserId"),
                df.parse(MapUtils.getString(criterion, "Start")),
                df.parse(MapUtils.getString(criterion, "End")));
        return _result == null ? new ArrayList() : _result;
    }

    /**
     * 保存日程数据。
     * <p>
     * @param instance 要保存的信息对象。
     * @return 新增记录主键.
     * @throws Exception
     */
    @RequestMapping(value = "/save.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> save(@RequestBody MyCalendarEntity instance) throws Exception {
        Map<String, Object> rtnVal = new HashMap();
        if (!"".equals(instance.getCalendarId())) {
            if (_manager.update(instance) > 0) {
                rtnVal.put("Success", true);
            } else {
                rtnVal.put("Success", false);
            }
        } else {
            rtnVal.put("HeaderKey", _manager.insert(instance));
        }

        return rtnVal;
    }

    /**
     * 删除日程信息数据。
     *
     * @param instance 要删除的数据
     * @return 返回删除后的数据
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "/delete.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Integer delete(@RequestBody MyCalendarEntity instance) throws Exception {
        return _manager.delete(instance);
    }
    // </editor-fold>
}
